package cn.petplatform.petback.service.pet.impl;

import cn.petplatform.dto.ResponseDto;
import cn.petplatform.petback.mapper.PetMapper;
import cn.petplatform.petback.mapper.PetPublishMapper;
import cn.petplatform.petback.pojo.Pet;
import cn.petplatform.petback.pojo.PetExample;
import cn.petplatform.petback.service.pet.PetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * @author LNL
 * @date 2021/6/4 15:18
 */
@Service
public class PetServiceImpl implements PetService {

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetPublishMapper petPublishMapper;

    @Autowired
    PetServiceImpl petService;

    @Override
    public ResponseDto getPets() {
        ResponseDto responseDto = null;
        List<Pet> pets = petMapper.selectByExample(new PetExample());
        Optional<List<Pet>> optionalPets = Optional.ofNullable(pets);
        if(optionalPets.isPresent()) {
            responseDto = new ResponseDto("1000","成功获取数据",pets);
        }else {
            responseDto = new ResponseDto("1001","获取数据失败");
        }
        return responseDto;
    }

    @Override
//    @Transactional
    public ResponseDto delete(String id) {
        petService.method1(id);
        System.out.println();
         throw new RuntimeException("");
//        return null;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public void method1 (String id) {

//        System.out.println("before");
        int update = petMapper.updateIsAdoptAsOne(id);
//        petService.method2(id);
//        System.out.println("htllo");
//        throw new RuntimeException("回滚");

    }
    @Transactional
    public void method2 (String id) {

//        System.out.println("before");
        int update = petMapper.updateIsAdoptAsOne(id);
//        System.out.println("htllo");
//        throw new RuntimeException("回滚");

    }


}
